package cst.a931xmc.yuejian20.Model;

import java.util.List;

import cst.a931xmc.yuejian20.Bean.ClassBean;
import cst.a931xmc.yuejian20.Interface.ClassIface;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Service.HomeService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ClassModel<T> extends ApiModel implements ClassIface {
    private Retrofit retrofit;
    private HomeService service;

    public ClassModel(){
        retrofit = new Retrofit
                .Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(HomeService.class);

    }

    @Override
    public void getClassListByMaster(int master_id, final Listener l) {
        Call<List<ClassBean>> call = service.getClassListByMaster(master_id);
        call.enqueue(new Callback<List<ClassBean>>() {
            @Override
            public void onResponse(Call<List<ClassBean>> call, Response<List<ClassBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<ClassBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getClassListByClub(int club_id, final Listener l) {
        Call<List<ClassBean>> call = service.getClassListByClub(club_id);
        call.enqueue(new Callback<List<ClassBean>>() {
            @Override
            public void onResponse(Call<List<ClassBean>> call, Response<List<ClassBean>> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<ClassBean>> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }

    @Override
    public void getDetail(int class_id, final Listener l) {
        Call<ClassBean> call = service.getClassDetail(class_id);
        call.enqueue(new Callback<ClassBean>() {
            @Override
            public void onResponse(Call<ClassBean> call, Response<ClassBean> response) {
                l.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<ClassBean> call, Throwable t) {
                l.onFail(t.toString());
            }
        });
    }
}
